package com.hsc.account;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.hsc.db.tools.AccountList;
import com.hsc.db.tools.AccountService;
import com.hsc.db.tools.EmailService;
import com.hsc.db.tools.ItemList;
import com.hsc.db.tools.ItemService;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.format.Time;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;


public class addemail extends Dialog {

	public addemail(Context context) {
		super(context);
	}

	public addemail(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private Context context;
		private String title;
		private String message;
		private String positiveButtonText;
		private String negativeButtonText;
		private View contentView;
		private DialogInterface.OnClickListener positiveButtonClickListener;
		private DialogInterface.OnClickListener negativeButtonClickListener;
		private  EditText  txtSend;
		private  EditText  txtPwd;
		private  EditText  txtRPwd;
		private  EditText  txtRecv;
		public Builder(Context context) {
			this.context = context;
		}

		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		/**
		 * Set the Dialog message from resource
		 *
		 * @param title
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		/**
		 * Set the Dialog title from resource
		 *
		 * @param title
		 * @return
		 */
		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		/**
		 * Set the Dialog title from String
		 *
		 * @param title
		 * @return
		 */

		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		/**
		 * Set the positive button resource and it's listener
		 *
		 * @param positiveButtonText
		 * @return
		 */
		public Builder setPositiveButton(int positiveButtonText,
										 DialogInterface.OnClickListener listener) {
			this.positiveButtonText = (String) context
					.getText(positiveButtonText);
			this.positiveButtonClickListener = listener;

			return this;
		}

		public Builder setPositiveButton(String positiveButtonText,
										 DialogInterface.OnClickListener listener) {
			this.positiveButtonText = positiveButtonText;
			this.positiveButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(int negativeButtonText,
										 DialogInterface.OnClickListener listener) {
			this.negativeButtonText = (String) context
					.getText(negativeButtonText);
			this.negativeButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(String negativeButtonText,
										 DialogInterface.OnClickListener listener) {
			this.negativeButtonText = negativeButtonText;
			this.negativeButtonClickListener = listener;
			return this;
		}

		public addemail create(final ItemService items) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final addemail dialog = new addemail(context,R.style.Dialog);
			View layout = inflater.inflate(R.layout.addemail, null);
			dialog.addContentView(layout, new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

			txtSend = (EditText)layout.findViewById(R.id.sendemail);
			txtRecv = (EditText)layout.findViewById(R.id.recvemail);
			txtPwd = (EditText)layout.findViewById(R.id.password);

			ItemList item1 = items.getlDataFlag("1");
			ItemList item2 = items.getlDataFlag("2");
			ItemList item3 = items.getlDataFlag("3");



			if(item1!=null)
			{
				txtSend.setText(item1.getType());
			}

			if(item2!=null)
			{
				txtPwd.setText(item2.getType());
			}

			if(item3!=null)
			{
				txtRecv.setText(item3.getType());
			}



			txtSend.requestFocus();

			openInputMethod(txtSend);

			((Button) layout.findViewById(R.id.btnQuxiao))
					.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							dialog.dismiss();


						}
					});




			((Button) layout.findViewById(R.id.btnQueren))
					.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {

							items.deleteAll();

							ItemList item = new ItemList();

							item.setFlag("1");
							item.setType(txtSend.getText().toString().trim());

							items.save(item);

							ItemList item1 = new ItemList();

							item1.setFlag("2");
							item1.setType(txtPwd.getText().toString().trim());

							items.save(item1);

							ItemList item2 = new ItemList();

							item2.setFlag("3");
							item2.setType(txtRecv.getText().toString().trim());
							items.save(item2);

							dialog.dismiss();

							Toast.makeText(context, "添加成功", Toast.LENGTH_SHORT).show();

						}
					});



			dialog.setContentView(layout);


			return dialog;
		}

		public void openInputMethod(final EditText editText) {

			Timer timer = new Timer();

			timer.schedule(new TimerTask() {

				public void run() {

					InputMethodManager inputManager = (InputMethodManager) editText

							.getContext().getSystemService(

									Context.INPUT_METHOD_SERVICE);

					inputManager.showSoftInput(editText, 0);

				}

			}, 200);
		}

	}
}